package com.smart.campus.service.impl;

import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.SysUserMapper;
import com.smart.campus.dao.TeacherAttendanceMapper;
import com.smart.campus.dto.AttendanceStatistics;
import com.smart.campus.dto.SysUser;
import com.smart.campus.dto.TeacherAttendance;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.TeacherAttendanceService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: smart-campus
 * @BelongsPackage: com.smart.campus.service.impl
 * @Author: JinBao shan
 * @date 2024/10/7 20:09
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class TeacherAttendanceServiceImpl implements TeacherAttendanceService {
    @Autowired
    private TeacherAttendanceMapper attendanceMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PagedResponse<TeacherAttendance> findTeacherAttendancePagination(TeacherAttendance attendance, Integer pageNum, Integer pageSize) {
        Page<TeacherAttendance> page = new Page<>(pageNum, pageSize);
        IPage<TeacherAttendance> teacherAttendancePagination = attendanceMapper.findTeacherAttendancePagination(page, attendance);
        return PaginationUtil.getPaginationResponse(teacherAttendancePagination.getRecords(),pageNum,pageSize,teacherAttendancePagination.getTotal());
    }

    @Override
    public void addTeacherAttendance(TeacherAttendance attendance) {
        attendance.setApprovalStatus(1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
        attendance.setCreateTime(format);
        attendance.setUpdateTime(format);
        attendance.setApprovalTime(format);
        attendanceMapper.insert(attendance);
    }

    @Override
    public TeacherAttendance getTeacherAttendanceById(Integer id) {
        return attendanceMapper.selectById(id);
    }

    @Override
    public void updateTeacherAttendance(TeacherAttendance attendance) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new java.util.Date(System.currentTimeMillis()));
        attendance.setUpdateTime(format);
        attendanceMapper.updateById(attendance);
    }

    @Override
    public void delTeacherAttendanceById(Integer id) {
        attendanceMapper.deleteById(id);
    }

    @Override
    public List<AttendanceStatistics> queryAttendanceStatistics(AttendanceStatistics attendanceStatistics) {

        List<Map<String,Object>> mapList = new ArrayList<>();
        mapList = attendanceMapper.queryAttendanceStatistics(attendanceStatistics);

        List<AttendanceStatistics> attendanceStatisticsList = new ArrayList<>();

        for (Map<String,Object> map:mapList) {
            AttendanceStatistics att = new AttendanceStatistics();
            String user_id = String.valueOf(map.get("user_id"));
            SysUser user = sysUserMapper.selectUserDept(Long.valueOf(user_id));
            att.setUserName(String.valueOf(map.get("user_name")));
            att.setDepartmentName(user.getShDepartment().getDeptName());
            att.setAttendanceDay(String.valueOf(map.get("age1")));
            att.setBeLate(Integer.valueOf(String.valueOf(map.get("age2"))));
            att.setLeaveEarly(Integer.valueOf(String.valueOf(map.get("age3"))));
            att.setAbsence(Integer.valueOf(String.valueOf(map.get("age4"))));
            att.setFieldPersonnelDay(Integer.valueOf(String.valueOf(map.get("age5"))));
            att.setAskForLeaveDay(Integer.valueOf(String.valueOf(map.get("age6"))));
            attendanceStatisticsList.add(att);
        }
        return attendanceStatisticsList;
    }
}
